package com.igeek.compus.study.menu.controller;

import cn.hutool.core.util.StrUtil;
import com.igeek.compus.study.menu.entity.Menu;
import com.igeek.compus.study.menu.service.MenuService;
import com.igeek.compus.study.system.Constants;
import org.apache.log4j.Logger;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menu")
public class MenuController {

    private final static Logger logger = Logger.getLogger(MenuController.class);

    @Autowired
    private MenuService menuService;


    @RequestMapping("updateMenuById.do")
    @ResponseBody
    public void updateMenuById(Menu menu){
        menuService.updateMenuById(menu);
    }

    @RequestMapping("list.do")
    @ResponseBody
    public Map list(Menu menu) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 业务代码，菜单信息查询
            List<Menu> menuList = menuService.getMenuListByPage(menu);
            int count = menuService.getMenuListCount(menu);

            result.put("data", menuList);
            result.put("count", count);

            result.put("code", 0);
            result.put("msg", "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 1);
            result.put("msg", "系统异常");
        }

        return result;

    }

    @RequestMapping("deleteMenuById.do")
    @ResponseBody
    public void deleteMenuById(Menu menu) {
        menuService.deleteMenuById(menu);
    }

    @RequestMapping("deleteMenuList.do")
    @ResponseBody
    public Map<String, Object> deleteMenuList(String menuIds) {
        Map<String, Object> result = new HashMap<>();

        List<String> menuIdList = Arrays.asList(menuIds.substring(1, menuIds.length() - 1).split(","));

        // 2.执行业务
        try {
            if (menuService.deleteMenuList(menuIdList) > 0) {
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            } else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }

        // 3.返回结果
        return result;
    }

    @RequestMapping("addMenu.do")
    @ResponseBody
    public Map<String, Object> addMenu(Menu menu) {
        Map<String, Object> result = new HashMap<>();

        // 1.参数校验
        if (StrUtil.isEmpty(menu.getMenuName()) || StrUtil.isEmpty(menu.getUrl())
                || menu.getSerial() == null) {
            result.put(Constants.RETCODE, Constants.ERROR_CODE);
            result.put(Constants.RETMSG, Constants.PARAM_ERROR_MSG);
            return result;
        }

        // 2.执行业务
        try {
            if (menuService.addMenu(menu) > 0) {
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            } else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }

        // 3.返回结果
        return result;
    }

    @RequestMapping("toMenuEdit.do")

    public ModelAndView toMenuEdit(Integer id) {
        // 通过id查询menu信息
        Menu menu = menuService.getMenuInfoById(id);


        Map<String, Object> map = new HashMap<>();
        map.put("id", menu.getId());
        map.put("menuName", menu.getMenuName());
        map.put("imageUrl", menu.getImageUrl());
        map.put("url", menu.getUrl());
        map.put("serial", menu.getSerial());

        return new ModelAndView("/menu/menu_edit", map);
    }

    @RequestMapping("updateMenu.do")
    @ResponseBody
    public Map<String, Object> updateMenu(Menu menu) {
        Map<String, Object> result = new HashMap<>();

        // 1.参数校验
        if (menu.getId() == null) {
            result.put(Constants.RETCODE, Constants.ERROR_CODE);
            result.put(Constants.RETMSG, Constants.PARAM_ERROR_MSG);
            return result;
        }

        // 2.执行业务
        try {
            if (menuService.updateMenuById(menu) > 0) {
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            } else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }

        // 3.返回结果
        return result;
    }

    @RequestMapping("export.do")
    @ResponseBody
    public void export(HttpServletResponse response) throws Exception {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet sheet = hssfWorkbook.createSheet();
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("菜单名称");

        response.setHeader("content-disposition", "attachment;filename=menuList.xls");
        OutputStream outputStream = response.getOutputStream();
        hssfWorkbook.write(outputStream);

        outputStream.flush();
        outputStream.close();

    }
}
